package cup.league.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class NewChampActivity extends Activity{
	
	private Button next;
	private EditText champName;
	private EditText numPart;
	private RadioGroup type;
	private RadioButton groups;
	private RadioButton playoffs;
	private RadioButton both;
	private CheckBox preLoadTeams;
	private CheckBox roundMatches;
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newchamp);
		
		this.roundMatches = (CheckBox) findViewById(R.id.roundMatches);
		this.roundMatches.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(NewChampActivity.this, "This option allow rounds of two matches with the same teams, one Home one Away", Toast.LENGTH_SHORT).show();
				
			}
		});
		
		this.preLoadTeams = (CheckBox) findViewById(R.id.preLoaded);
		this.preLoadTeams.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Toast.makeText(NewChampActivity.this, "test", Toast.LENGTH_SHORT).show();
				
			}
		});
		
		try{
			this.champName = (EditText) findViewById(R.id.championshipName);
			this.numPart = (EditText) findViewById(R.id.numparticipants);
			this.groups = (RadioButton) findViewById(R.id.groups);
			this.playoffs = (RadioButton) findViewById(R.id.playoffs);
			this.both = (RadioButton) findViewById(R.id.both);
			
			this.next = (Button) findViewById(R.id.next);
			this.next.setOnClickListener(new OnClickListener() {
				
				int nPart = 0;
			
				public boolean formValidation(String cName, boolean pre, boolean gro, boolean pla, boolean bot){
					
					try{
						nPart = Integer.parseInt(numPart.getText().toString());
					}
					catch(NumberFormatException ne){
						Toast.makeText(NewChampActivity.this, "ERROR"+ne.toString(), Toast.LENGTH_SHORT).show();
					}
					if (cName.contentEquals("") || (nPart <= 1) || (gro == false && pla == false && bot == false))
						return false;
					else
						return true;
					
				}
				
				public void onClick(View v) {
					String cName = champName.getText().toString();
					boolean pre = preLoadTeams.isChecked();
					boolean gro = groups.isChecked();
					boolean pla = playoffs.isChecked();
					boolean bot = both.isChecked();
					boolean test = false;
					
					if(formValidation(cName, pre, gro, pla, bot)){
						DBHandler db = new DBHandler(NewChampActivity.this);
						db.open();
						if(gro)
							if(db.addChamp(cName, "Groups", nPart))
								test = true;
						else if (pla)
							if(db.addChamp(cName, "Playoffs", nPart))
								test = true;
						else if (bot)
							if(db.addChamp(cName, "Both", nPart))
								test = true;
						
						if(!test)
							Toast.makeText(NewChampActivity.this, "An error ocurred.", Toast.LENGTH_SHORT).show();
						else{
							Toast.makeText(NewChampActivity.this, "Championship registered succesfully", Toast.LENGTH_SHORT).show();
							if(pre)
								lounchSelectTeams(db.getIdChampByName(cName), nPart);
							else
								lounchRegisterTeams(db.getIdChampByName(cName), nPart);
						}
						db.close();
					}
					else
						Toast.makeText(NewChampActivity.this, "The form still have errors", Toast.LENGTH_SHORT).show();
				}
			});
		}
		catch(Exception e){
			Log.e("ERROR", "ERROR IN CODE:"+ e.toString());
			e.printStackTrace();
		}
	}
	
	public void lounchRegisterTeams(int champId, int numPart){
		try{
			Intent i = new Intent(NewChampActivity.this, RegisterTeamsActivity.class);
			i.putExtra("numPart", numPart);
			i.putExtra("champId", champId);
			startActivity(i);
		}
		catch(Exception e){
			Log.e("ERROR","ERROR: "+e.toString());
			e.printStackTrace();
		}
	}
	
	public void lounchSelectTeams(int champId, int numPart){
		
	}

}
